package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.MemberCardUseBO;
import com.junzhiit.nengji.dao.MemberCardUseDAO;
import com.junzhiit.nengji.model.MemberCardUse;

@Repository
public class MemberCardUseDAOImpl extends AbstractBaseDAO<MemberCardUse> implements MemberCardUseDAO {

	@Override
	public List<MemberCardUseBO> getAllByOrderNumber(String orderId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		
		sql.append("select mcu.*,mc.card_name from member_card_use mcu");
		sql.append(" left join member_card mc on mcu.member_card_id = mc.id ");
		sql.append(" where mcu.order_id = ? and mcu.is_delete = ? ");
		params.setString(orderId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql.toString(), MemberCardUseBO.class, params);
	}

	@Override
	public List<MemberCardUseBO> findAll(MemberCardUseBO memberCardUse, int pageNumber, int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		
		sql.append(" select mcu.*,mc.card_name cardName, ");
		sql.append(" (case when u.`name` is null then u.nick_name else u.`name` end) userName, ");
		sql.append(" o.status,o.shop_order_number from member_card_use mcu ");
		sql.append(" left join member_card mc on mcu.member_card_id = mc.id ");
		sql.append(" left join user u on mcu.user_id = u.id ");
		sql.append(" left join orders o on mcu.order_id = o.id or mcu.order_number = o.order_number ");
		sql.append(" where mcu.is_delete = ? ");
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(memberCardUse.getCsId())){
			sql.append(" and mcu.cs_id = ? ");
			params.setString(memberCardUse.getCsId());
		}
		if(StringUtils.isNotBlank(memberCardUse.getOrderId())){
			sql.append(" and mcu.order_id = ? ");
			params.setString(memberCardUse.getOrderId());
		}
		if(StringUtils.isNotBlank(memberCardUse.getShopOrderNumber())){
			sql.append(" and o.shop_order_number = ? ");
			params.setString(memberCardUse.getShopOrderNumber());
		}
		if(StringUtils.isNotBlank(memberCardUse.getCardName())){
			sql.append(" and mc.card_name like ? ");
			params.setString("%"+memberCardUse.getCardName()+"%");
		}
		if(StringUtils.isNotBlank(memberCardUse.getUserName())){
			sql.append(" and u.name like ? ");
			params.setString("%"+memberCardUse.getUserName()+"%");
		}
		if(memberCardUse.getPrivilegeType()!=null){
			sql.append(" and mcu.privilege_type = ? ");
			params.setInt(memberCardUse.getPrivilegeType());
		}
		if(memberCardUse.getPrivilegeMoney()!=null){
			sql.append(" and mcu.privilege_money = ? ");
			params.setDouble(memberCardUse.getPrivilegeMoney());
		}
		return super.queryForPagingList(sql.toString(), MemberCardUseBO.class, params, pageNumber,maxPageRow);
	}

	@Override
	public int getTotalCount(MemberCardUseBO memberCardUse) {
		StringBuffer sql=new StringBuffer();
		StatementParameter params=new StatementParameter();
		
		sql.append(" select count(*) from member_card_use mcu ");
		sql.append(" left join member_card mc on mcu.member_card_id = mc.id ");
		sql.append(" left join user u on mcu.user_id = u.id ");
		sql.append(" left join orders o on mcu.order_id = o.id ");
		sql.append(" where mcu.is_delete = ? ");
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(memberCardUse.getCsId())){
			sql.append(" and mcu.cs_id = ? ");
			params.setString(memberCardUse.getCsId());
		}
		if(StringUtils.isNotBlank(memberCardUse.getOrderId())){
			sql.append(" and mcu.order_id = ? ");
			params.setString(memberCardUse.getOrderId());
		}
		if(StringUtils.isNotBlank(memberCardUse.getShopOrderNumber())){
			sql.append(" and o.shop_order_number = ? ");
			params.setString(memberCardUse.getShopOrderNumber());
		}
		if(StringUtils.isNotBlank(memberCardUse.getCardName())){
			sql.append(" and mc.card_name like ? ");
			params.setString("%"+memberCardUse.getCardName()+"%");
		}
		if(StringUtils.isNotBlank(memberCardUse.getUserName())){
			sql.append(" and u.name like ? ");
			params.setString("%"+memberCardUse.getUserName()+"%");
		}
		if(memberCardUse.getPrivilegeType()!=null){
			sql.append(" and mcu.privilege_type = ? ");
			params.setInt(memberCardUse.getPrivilegeType());
		}
		if(memberCardUse.getPrivilegeMoney()!=null){
			sql.append(" and mcu.privilege_money = ? ");
			params.setDouble(memberCardUse.getPrivilegeMoney());
		}
		return super.queryForInt(sql.toString(), params);
	}

	
}
